
<!-- 右 -->
<div class="content">
    <div class="header">
        <h1 class="page-title">角色修改</h1>
    </div>

    <div class="well">
        <!-- add form -->
        <form action="{:url('/admin/rule/update')}" method="post" id="tab">
            {:token()}
            <input type="hidden" name="id" value="{$data.id}">
            <label>用户组</label>
            <input type="text" name="role_name" id="role_name" value="{$data.role_name}" class="input-xlarge">
            <!-- table -->
            <table class="table table-bordered table-hover table-condensed">
                <thead>
                <tr>
                    <th>权限分类</th>
                    <th>权限</th>
                </tr>
                </thead>
                <tbody>
                    {foreach $uptree as $v}
                        <tr class="success">
                            <td>
                                <input type="checkbox" class="top_auth" name="role_ids[]" value="{$v.id}"
                                {in name='$v.id' value="$data.role_ids" } checked {/in}
                                >{$v.auth_name}
                            </td>
                            <td>
                                {foreach $downtree as $m}
                                    {if($m.pid == $v.id)}
                                        <input type="checkbox" class="down_auth" name="role_ids[]" value="{$m.id}"
                                           {in name='$m.id' value="$data.role_ids" } checked {/in} >{$m.auth_name}
                                    {/if}
                                {/foreach}
                            </td>
                        </tr>
                    {/foreach}
                </tbody>
            </table>

            <label></label>
            <button class="btn btn-primary" type="button">保存</button>
        </form>
    </div>
</div>
<script>
        // 给一级权限绑定事件
        $(".top_auth").change(function(){

            var status = $(this).prop('checked');
            $(this).closest('tr').find('.down_auth').prop('checked',status);
        });

        // 给所有的二级权限绑定事件
        $(".down_auth").change(function(){
            var checked = $(this).closest('tr').find('.down_auth:checked').length;

            if(checked > 0){
                $(this).closest('tr').find('.top_auth').prop('checked',true);
            }
        })

        // 提交时间
        $(".btn-primary").click(function(){
            var flag = true;

            if($("#role_name").val() == ""){
                alert("亲！不允许为空哦");
                flag = false;
            }

            if(flag){
                $("#tab").submit();
            }
        })

</script>